/* 
 * Cashbox is the legal property of its developers, whose names are listed in the copyright file included
 * with this source distribution.
 * 
 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
 * General Public License as published by the Free Software Foundation; either version 2 of the License,
 * or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
 * Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with this program; if not,
 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

#import "CBImportAccount.h"
#import "CBAccount.h"
#import <Cashbox/CBAccountControllerProtocol.h>

@interface CBImportAccount (Private)

- (id)initWithName:(NSString *)name keys:(NSArray *)keys reference:(id <NSObject>)reference;
- (void)setPossibleTypes:(NSArray *)array;
- (void)setPossibleAccounts:(NSArray *)array;

@end

@implementation CBImportAccount

+ (CBImportAccount *)importAccountWithName:(NSString *)name keys:(NSArray *)keys reference:(id <NSObject>)reference {
	return [[[self alloc] initWithName:name keys:(NSArray *)keys reference:reference] autorelease];
}

- (id)init {
	if (self = [super init]) {
		import = TRUE;
	}
	return self;
}

- (id)initWithName:(NSString *)aName keys:(NSArray *)keyArray reference:(id <NSObject>)aReference {
	if (self = [self init]) {
		[self setName:aName];
		[self setKeys:keyArray];
		[self setReference:aReference];
	}
	return self;
}

- (void)dealloc {
	[name release];
	[reference release];
	[mergeAccount release];
	[keys release];
	[possibleTypes release];
	[possibleAccounts release];
	
	[super dealloc];
}

- (void)didChangeValueForKey:(NSString *)key {
	if ([key isEqualToString:@"keys"]) {
		// regenerate possible types
		[self setPossibleTypes:[[cashbox accountController] accountTypesWithAllImportKeys:[self keys]]];
		// regenerate possible accounts
		[self setPossibleAccounts:[CBAccount accountsWithAnyAccountType:[self possibleTypes]]];
	}
	[super didChangeValueForKey:key];
}

- (NSString *)name {
	return name;
}
- (void)setName:(NSString *)aName {
	if (name != aName) {
		[self willChangeValueForKey:@"name"];
		[name release];
		name = [aName retain];
		[self didChangeValueForKey:@"name"];
	}
}

- (NSArray *)keys {
	return keys;
}
- (void)setKeys:(NSArray *)array {
	if (keys != array) {
		[self willChangeValueForKey:@"keys"];
		[keys release];
		keys = [array retain];
		[self didChangeValueForKey:@"keys"];
	}	
}

- (id <NSObject>)reference {
	return reference;
}
- (void)setReference:(id <NSObject>)aReference {
	if (reference != aReference) {
		[self willChangeValueForKey:@"reference"];
		[reference release];
		reference = [aReference retain];
		[self didChangeValueForKey:@"reference"];
	}	
}

- (BOOL)import {
	return import;
}
- (void)setImport:(BOOL)flag {
	[self willChangeValueForKey:@"import"];
	import = flag;
	[self didChangeValueForKey:@"import"];
}

- (Class)createAccount {
	return createAccount;
}
- (void)setCreateAccount:(Class)accountType {
	if (createAccount != accountType) {
		[self willChangeValueForKey:@"createAccount"];
		createAccount = accountType;
		[self didChangeValueForKey:@"createAccount"];
		
		// invalidate merge account
		[self willChangeValueForKey:@"mergeAccount"];
		[mergeAccount release];
		mergeAccount = nil;
		[self didChangeValueForKey:@"mergeAccount"];
	}	
}

- (NSArray *)possibleTypes {
	return possibleTypes;
}
- (void)setPossibleTypes:(NSArray *)array {
	if (possibleTypes != array) {
		[self willChangeValueForKey:@"possibleTypes"];
		[possibleTypes release];
		possibleTypes = [array retain];
		[self didChangeValueForKey:@"possibleTypes"];
	}	
}

- (CBAccount *)mergeAccount {
	return mergeAccount;
}
- (void)setMergeAccount:(CBAccount *)account {
	if (mergeAccount != account) {
		[self willChangeValueForKey:@"mergeAccount"];
		[mergeAccount release];
		mergeAccount = [account retain];
		[self didChangeValueForKey:@"mergeAccount"];
		
		
		// invalidate create type
		[self willChangeValueForKey:@"createAccount"];
		createAccount = nil;
		[self didChangeValueForKey:@"createAccount"];
	}	
}

- (NSArray *)possibleAccounts {
	return possibleAccounts;
}
- (void)setPossibleAccounts:(NSArray *)array {
	if (possibleAccounts != array) {
		[self willChangeValueForKey:@"possibleAccounts"];
		[possibleAccounts release];
		possibleAccounts = [array retain];
		[self didChangeValueForKey:@"possibleAccounts"];
	}	
}

@end
